const db = require("../config/db");

exports.getAll = async () => {
  const [rows] = await db.query("SELECT * FROM merchants");
  return rows;
};

exports.getRegions = async () => {
  const [rows] = await db.query("SELECT DISTINCT region FROM merchants");
  return rows.map((r) => r.region);
};

exports.approve = async (id) => {
  await db.query("UPDATE merchants SET status = ? WHERE id = ?", [
    "approved",
    id,
  ]);
  const [rows] = await db.query("SELECT * FROM merchants WHERE id = ?", [id]);
  return rows[0];
};

exports.reject = async (id, remark) => {
  await db.query("UPDATE merchants SET status = ?, remark = ? WHERE id = ?", [
    "rejected",
    remark,
    id,
  ]);
  const [rows] = await db.query("SELECT * FROM merchants WHERE id = ?", [id]);
  return rows[0];
};
